﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using Zhao.Common.Helper;
using Zhao.IServer.Field;
using Zhao.Models.DTO;
using Zhao.Models.Entity;

namespace Zhao.Arena.Controllers.Field
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class FacilityTimePriceController : ControllerBase
    {
        private readonly IFacilityTimePriceServer _priceServer;
        private readonly IFieldTypesServer _fieldTypesServer;

        public FacilityTimePriceController(IFacilityTimePriceServer priceServer, IFieldTypesServer fieldTypesServer)
        {
            _priceServer = priceServer;
            _fieldTypesServer = fieldTypesServer;
        }

        [HttpGet]
        [Route("{tyepcId:int}")]
        public async Task<JsonResult> GetFtpInfos(int tyepcId)
        {

            var result = new ApiResult<List<FTimePriceDto>>
            {
                Success = false,
                Message = "操作失败"
            };

            string? venueIdClaim = User.FindFirst("VenueId").Value;

            if (!Guid.TryParse(User.FindFirst("VenueId")?.Value, out var venueId))
            {
                //如果没有就是管理员查询不是球馆管理员 直接查询全部
                venueId = default;
            }


            Expressionable<FacilityTimePrice, FieldTypes> expressionable = new Expressionable<FacilityTimePrice, FieldTypes>();

            expressionable.AndIF(venueId != default, (a, b) => a.VenueID.Equals(venueId));

            result.Data = await _priceServer.GetInfos(expressionable);

            if (result.Data != null)
            {
                result.Success = true;
                result.Message = "操作成功";
            }

            return new JsonResult(result);
        }

        /// <summary>
        /// 添加时间段价格
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> AddFacilityTimePrice([FromBody] FTimePriceDto dto)
        {
            var result = new ApiResult
            {
                Success = false,
                Message = "操作失败",
            };

            // 参数基础校验
            if (dto == null)
            {
                result.Message = "参数不能为空";
                return new JsonResult(result);
            }

            if (dto.startTime >= dto.endTime)
            {
                result.Message = "开始时间必须早于结束时间";
                return new JsonResult(result);
            }

            if (dto.price <= 0)
            {
                result.Message = "价格必须大于0";
                return new JsonResult(result);
            }

            if (!Guid.TryParse(User.FindFirst("VenueId")?.Value, out var venueId))
            {
                //如果没有就是管理员查询不是球馆管理员 直接查询全部
                venueId = default;
            }

            // 调用服务层处理
            var serviceResult = await _priceServer.AddFacilityTimePriceAsync(dto, venueId);

            // 转换返回结果
            result.Success = serviceResult.Success;
            result.Message = serviceResult.Message;

            return new JsonResult(result);
        }


        /// <summary>
        /// 修改时间段价格
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        public async Task<JsonResult> UpdateFacilityTimePrice([FromBody] FTimePriceDto dto)
        {
            var result = new ApiResult
            {
                Success = false,
                Message = "操作失败",
            };

            // 参数基础校验
            if (dto == null)
            {
                result.Message = "参数不能为空";
                return new JsonResult(result);
            }

            if (dto.id <= 0)
            {
                result.Message = "ID参数无效";
                return new JsonResult(result);
            }

            if (dto.startTime >= dto.endTime)
            {
                result.Message = "开始时间必须早于结束时间";
                return new JsonResult(result);
            }

            if (dto.price <= 0)
            {
                result.Message = "价格必须大于0";
                return new JsonResult(result);
            }

            if (!Guid.TryParse(User.FindFirst("VenueId")?.Value, out var venueId))
            {
                //如果没有就是管理员查询不是球馆管理员 直接查询全部
                venueId = default;
            }

            // 调用服务层处理
            var serviceResult = await _priceServer.UpdateFacilityTimePriceAsync(dto, venueId);

            // 转换返回结果
            result.Success = serviceResult.Success;
            result.Message = serviceResult.Message;

            return new JsonResult(result);
        }
    }
}
